/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sistema_fuzzy;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author Thay
 */
public class InfraVermelho {

    private int distancia;
    static private int alcance = 100;
    private Point obstaculoMaisProximo;

    public InfraVermelho() {
        distancia  = Integer.MAX_VALUE;
    }

    public int getDistancia() {
        return distancia;
    }

    public void setDistancia(int distancia) {
        this.distancia = distancia;
    }

    public Point getObstaculoMaisProximo() {
        return obstaculoMaisProximo;
    }

    public void setObstaculoMaisProximo(Point obstaculoMaisProximo) {
        this.obstaculoMaisProximo = obstaculoMaisProximo;
    }

    public static List<Point> ordenaX(List<Point> obstaculos) {

        List<Point> obstaculosX = new ArrayList<Point>(obstaculos);
        Collections.sort(obstaculosX, new Comparator() {

            public int compare(Object o1, Object o2) {
                Point p1 = (Point) o1;
                Point p2 = (Point) o2;
                return p1.x - p2.x;
            }
        });
        return obstaculosX;
    }

    public static List<Point> ordenaY(List<Point> obstaculos) {

        List<Point> obstaculosY = new ArrayList<Point>(obstaculos);
        Collections.sort(obstaculosY, new Comparator() {

            public int compare(Object o1, Object o2) {
                Point p1 = (Point) o1;
                Point p2 = (Point) o2;
                return p1.y - p2.y;
            }
        });
        return obstaculosY;
    }

    public double calcularDistancia(Point robo, List<Point> obstaculos, int deslocamento) {

        if (!obstaculos.isEmpty()) {

            List<Point> obstaculosX = ordenaX(obstaculos);
            List<Point> obstaculosY = ordenaY(obstaculos);

            switch (deslocamento) {
                case 37:    // Seta para a ESQUERDA
                    for (int i = obstaculosX.size()-1; i >= 0; i--) {
                        obstaculoMaisProximo = obstaculosX.get(i);
                        if (robo.x >= obstaculoMaisProximo.x+50 &&
                            robo.y + 25 >= obstaculoMaisProximo.y &&
                            robo.y + 25 <= obstaculoMaisProximo.y + 50) {
                            distancia = Math.abs(obstaculoMaisProximo.x+50 - robo.x);
                                break;
                        } else distancia = Integer.MAX_VALUE;
                    }
                    break;
                case 38:    // Seta para CIMA
                    for (int i = obstaculosY.size()-1; i >= 0; i--) {
                        obstaculoMaisProximo = obstaculosY.get(i);
                        if (robo.y >= obstaculoMaisProximo.y+50 &&
                            robo.x + 25 >= obstaculoMaisProximo.x &&
                            robo.x + 25 <= obstaculoMaisProximo.x + 50) {
                            distancia = Math.abs(obstaculoMaisProximo.y+50 - robo.y);
                                break;
                        } else distancia = Integer.MAX_VALUE;
                    }
                    break;
                case 39:    // Seta para a DIREITA
                    for (int i = 0; i < obstaculosX.size(); i++) {
                        obstaculoMaisProximo = obstaculosX.get(i);
                        if (robo.x+50 <= obstaculoMaisProximo.x &&
                            robo.y + 25 >= obstaculoMaisProximo.y &&
                            robo.y + 25 <= obstaculoMaisProximo.y + 50) {
                            distancia = Math.abs(obstaculoMaisProximo.x - (robo.x+50));
                                break;
                        } else distancia = Integer.MAX_VALUE;
                    }
                    break;
                case 40:    // Seta para BAIXO
                    for (int i = 0; i < obstaculosY.size(); i++) {
                        obstaculoMaisProximo = obstaculosY.get(i);
                        if (robo.y+50 <= obstaculoMaisProximo.y &&
                            robo.x + 25 >= obstaculoMaisProximo.x &&
                            robo.x + 25 <= obstaculoMaisProximo.x + 50) {
                            distancia = Math.abs(obstaculoMaisProximo.y - (robo.y+50));
                                break;
                        } else distancia = Integer.MAX_VALUE;
                    }
                    break;
            }
            if(distancia < alcance)
                System.out.println("distancia: " + distancia);
            else
                distancia = Integer.MAX_VALUE;
        }

//        if (Math.abs(distancia) < alcance) {
//            System.out.println("distancia: " + distancia);

        return distancia;
    }
}
